/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;

import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.*;

/**
 * Main GUI file, containing code for GUI frame and various static methods
 */
public class GUI_Main extends JFrame
{
    /**
     * Admin Screens....................................................................10
     * Doctor Screens ..................................................................10
     * Nurse Screens ...................................................................10
     * Allow login of all types of users ...............................................05
     * Appropriate security applied to different users..................................05
     * Treatment Screens................................................................10
     * All other M6 functionality is present and working thru GUI (like Load/Save)......10
     * Networking Functioning (multiple clients) .......................................10
     * Patient chart report graph ......................................................05
     * Patient Invoice Screen...........................................................05
     * Good Java Style, Code Pro Audit Clean ...........................................10
     * Javadoc api......................................................................10
     */

    private JLabel titleLabel;
    private JTabbedPane centerPanel;

    private JPanel loginPanel;
    private JPanel loginPanel_centerPanel;
    private JTextField loginPanel_unField;
    private JPasswordField loginPanel_pwField;
    private JButton loginPanel_submitButton;
    //private static User login_attempt_user = null;
    private static int login_attempt_strikes = 0;
    private JLabel loginPanel_helpInfoLabel0;
    private JLabel loginPanel_helpInfoLabel1;
    private JEditorPane loginPanel_helpInfoLabel2;

    private JPanel adminMainPanel;
    private JPanel doctorMainPanel;
    private JPanel nurseMainPanel;
    
    private JPanel patientMainPanel;
    private JLabel patientMainPanel_nameField;
    private JButton patientMainPanel_logoutButton;
    private JButton patientMainPanel_requestAppointmentButton;

    private JPanel patientRequestAppointmentPanel;
    private java.util.Date patientRequestAppointmentPanel_date;
    private JSpinner patientRequestAppointmentPanel_dateSpinner;
    private JSpinner patientRequestAppointmentPanel_timeSpinner;
    private JButton patientRequestAppointmentPanel_submitButton;
    
    private JPanel treatmentViewPanel;
    private JPanel treatmentEditPanel;
    
    
    /* 
     * private SystemAdmin currentSysAdmin;
     * private Doctor currentDoctor;
     * private Nurse currentNurse;
     * private Patient currentPatient;
     * private Patient currentSubject; // patient being accessed by doctor or nurse.
     */


    /**
     * Creates a new instance of Main
     */
    public GUI_Main()
    {
        titleLabel = new JLabel("COUNTERSTRIKE HOSPITAL");
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        titleLabel.setFont(new Font(titleLabel.getFont().getName(), titleLabel.getFont().getStyle(), 50));
        
        // LOGIN PANEL
        
        loginPanel_unField = new JTextField();
        loginPanel_pwField = new JPasswordField();
        loginPanel_submitButton = new JButton("Submit");
        loginPanel_submitButton.addActionListener(new LoginPanel_SubmitListener());

        loginPanel_centerPanel = new JPanel();
        loginPanel_centerPanel.setLayout(new GridBagLayout());
        loginPanel_centerPanel.add(new JLabel("Username: "), GUI_Main.constraints(0, 0, 1, 1, 0.2, 1.0, new Insets(5, 5, 5, 5)));
        loginPanel_centerPanel.add(loginPanel_unField,       GUI_Main.constraints(1, 0, 1, 1, 1.0, 1.0, new Insets(5, 5, 5, 5)));
        loginPanel_centerPanel.add(new JLabel("Password: "), GUI_Main.constraints(0, 1, 1, 1, 0.2, 1.0, new Insets(5, 5, 5, 5)));
        loginPanel_centerPanel.add(loginPanel_pwField,       GUI_Main.constraints(1, 1, 1, 1, 1.0, 1.0, new Insets(5, 5, 5, 5)));
        loginPanel_centerPanel.add(loginPanel_submitButton,  GUI_Main.constraints(0, 2, 2, 1, 1.0, 1.0, new Insets(5, 5, 5, 5)));
        loginPanel_centerPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.BLUE), "Login"));

        loginPanel_helpInfoLabel0 = new JLabel("-Enter your username and password to access the site\n");
        loginPanel_helpInfoLabel1 = new JLabel("-For further assistance, call the system admin at 555.555.2340 or email \"it.s@over.9000\"");
        loginPanel_helpInfoLabel2 = new JEditorPane("text/html", "<span style=\"font: '" + loginPanel_helpInfoLabel1.getFont().getFontName()
                                                    + "'; font-size: " + loginPanel_helpInfoLabel1.getFont().getSize()
                                                    + "pt; font-weight:bold\">Visit <a href=\"http://www.youtube.com/watch?v=oHg5SJYRHA0\">our website</a> for more information.</span>");
        loginPanel_helpInfoLabel2.setEditable(false);
        loginPanel_helpInfoLabel2.setOpaque(false);
        loginPanel_helpInfoLabel2.addHyperlinkListener(new ClickedHyperlinkListener());
        loginPanel_helpInfoLabel2.setFont(loginPanel_helpInfoLabel1.getFont());

        loginPanel = new JPanel();
        loginPanel.setLayout(new GridBagLayout());
        loginPanel.add(new JLabel(""),            GUI_Main.constraints(0, 0, 3, 1, 1.0, 1.0));
        loginPanel.add(new JLabel(""),            GUI_Main.constraints(0, 1, 1, 1, 1.0, 0.0));
        loginPanel.add(loginPanel_centerPanel,    GUI_Main.constraints(1, 1, 1, 1, 1.0, 0.4));
        loginPanel.add(new JLabel(""),            GUI_Main.constraints(2, 1, 1, 1, 1.0, 0.0));
        loginPanel.add(new JLabel(""),            GUI_Main.constraints(0, 2, 3, 1, 1.0, 1.0));
        loginPanel.add(loginPanel_helpInfoLabel0, GUI_Main.constraints(0, 3, 3, 1, 1.0, 0.0));
        loginPanel.add(loginPanel_helpInfoLabel1, GUI_Main.constraints(0, 4, 3, 1, 1.0, 0.0));
        loginPanel.add(loginPanel_helpInfoLabel2, GUI_Main.constraints(0, 5, 3, 1, 1.0, 0.0));
        loginPanel.add(new JLabel(""),            GUI_Main.constraints(0, 6, 3, 1, 1.0, 1.0));
        
        // PATIENT PANEL

        /*Patient - A patient can do the following things:
log on to the system
request an appointment
view their medical history
Nurse - A nurse can do the following things:
create, delete and view patients
lookup a patient by name and edit their details
create a new treatment record for the patient
can view the daily appointment page
Doctor - A doctor can do the following things:
can do everything a nurse can do
can add doctors orders to the treatment record.
can view reports and statistics
System Administrator - A system administrator can do the following things:
can do everything any other use can do EXCEPT fill out doctors orders.
can add, edit and delete users in the system
can set or reset passwords for users
can suspend and unsuspend accounts
         */




        centerPanel = new JTabbedPane();
        centerPanel.setTabPlacement(JTabbedPane.BOTTOM);
        centerPanel.add("Login", loginPanel);
        //centerPanel.add("Patient Main", new GUI_PatientMainPanel());
        centerPanel.add("Patient Request Appointment", new GUI_PatientRequestAppointmentPanel());

        setLayout(new GridBagLayout());
        add(titleLabel,     GUI_Main.constraints(0, 0, 3, 1, 1.0, 0.0));
        add(centerPanel,    GUI_Main.constraints(0, 1, 3, 1, 1.0, 1.0));

        setTitle("Test Login Page");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public void launch()
    {
        setVisible(true);
    }
    public void close()
    {
        setVisible(false);
    }

    /**
     * Sets the visibility of panels based on the login state.
     * If no one is logged in, the LoginPanel is visible, else the respective SysAdmin, Doctor, Nurse, or Patient panel is visible;
     */
    public void resetPanels()
    {
        closeAllPanels();
        loginPanel.setVisible(true);
        /*
        if ( currentSysAdmin == null && currentDoctor == null && currentNurse == null && currentPatient == null )
        {
            loginPanel.setVisible(true);
        }
        else if ( currentSysAdmin != null && currentDoctor == null && currentNurse == null && currentPatient == null )
        {
            adminMainPanel.setVisible(true);
        }
        else if ( currentSysAdmin == null && currentDoctor != null && currentNurse == null && currentPatient == null )
        {
            doctorMainPanel.setVisible(true);
        }
        else if ( currentSysAdmin == null && currentDoctor == null && currentNurse != null && currentPatient == null )
        {
            nurseMainPanel.setVisible(true);
        }
        else if ( currentSysAdmin == null && currentDoctor == null && currentNurse == null && currentPatient != null )
        {
            patientMainPanel.setVisible(true);
        }
        else
        {
            loginPanel.setVisible(true);
        }
         */
    }
    /**
     * Sets the visibility of all panels to false
     */
    private void closeAllPanels()
    {
        loginPanel.setVisible(false);
        adminMainPanel.setVisible(false);
        doctorMainPanel.setVisible(false);
        nurseMainPanel.setVisible(false);
        patientMainPanel.setVisible(false);
    }
    
    /**
     * An ActionListener to attempt to login
     */
    private class LoginPanel_SubmitListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            /*
            if ( Login.loginUser(loginPanel_unField.getText(), String.valueOf(loginPanel_pwField.getPassword())) )
            {
                // set respective user as logged in user
                login_attempt_strikes = 0;
                loginPanel_unField.setEnabled(false);
                loginPanel_pwField.setEnabled(false);
                loginPanel_pwField.setText("");
                loginPanel_submitButton.setText("Log out");
            }
            else
            {
                if ( login_attempt_user == Login.getUser(loginPanel_unField.getText()) )
                {
                    if ( ++login_attempt_strikes == 3 )
                    {
                        Login.changeUserSuspendState(login_attempt_user, false);
                    }
                }
                else
                {
                    login_attempt_strikes = 0;
                    login_attempt_user = Login.getUser(loginPanel_unField.getText());
                }
            }
             */
        }
    }
    
    /**
     * A HyperlinkListener to open links in a JEditorPane.
     */
    private class ClickedHyperlinkListener implements HyperlinkListener
    {
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
            if ( HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType()) )
            {
                if( !java.awt.Desktop.isDesktopSupported() )
                {
                    System.err.println( "Desktop is not supported (fatal)" );
                }
                else if ( !java.awt.Desktop.getDesktop().isSupported(java.awt.Desktop.Action.BROWSE))
                {
                    System.err.println( "Desktop doesn't support the browse action (fatal)" );
                }
                else
                {
                    try
                    {
                        java.net.URI uri = new java.net.URI(e.getURL().toString());
                        java.awt.Desktop.getDesktop().browse(uri);
                    }
                    catch ( Exception ex )
                    {
                        System.err.println( ex.getMessage() );
                    }
                }
            }
        }
    }
    
    /**
     * Returns a GridBagConstraints with the given parameters, insets of 5 on all sides, ipadx 0, and ipady 0
     * @param posx the x coordinate
     * @param posy the y coordinate
     * @param lenx the length in the x direction
     * @param leny the length in the y direction
     * @param weightx the weight in the x direction
     * @param weighty the weight in the y direction
     * @return the GridBagConstraints with the specified parameters
     */
    public static GridBagConstraints constraints(int posx, int posy, int lenx, int leny, double weightx, double weighty)
    {
        return new GridBagConstraints(posx, posy, lenx, leny, weightx, weighty, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);
    }
    
    /**
     * Returns a GridBagConstraints with the given parameters, ipadx 0, and ipady 0.
     * @param posx the x coordinate
     * @param posy the y coordinate
     * @param lenx the length in the x direction
     * @param leny the length in the y direction
     * @param weightx the weight in the x direction
     * @param weighty the weight in the y direction
     * @param i the insets
     * @return the GridBagConstraints with the specified parameters
     */
    public static GridBagConstraints constraints(int posx, int posy, int lenx, int leny, double weightx, double weighty, Insets i)
    {
        return new GridBagConstraints(posx, posy, lenx, leny, weightx, weighty, GridBagConstraints.CENTER, GridBagConstraints.BOTH, i, 0, 0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        GUI_Main m = new GUI_Main();
    }

}

